package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.LabelResVo;
import com.miplus.ccrm.core.controller.vo.aicall.ImpObCallReqVo;
import com.miplus.ccrm.core.controller.vo.aicall.ImpObCaseResVo;
import com.miplus.ccrm.core.controller.vo.aiqm.*;
import com.miplus.ccrm.core.controller.vo.ficase.ImpResultVo;
import com.miplus.ccrm.core.controller.vo.strategy.PreViewTrigReqVo;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.aiqm.QmDataService;
import com.miplus.ccrm.core.service.aiqm.QmGradeService;
import com.miplus.ccrm.core.service.aiqm.QmRulesService;
import com.miplus.ccrm.core.service.aiqm.QmTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-11.智能质检服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/aiqm")
public class AiQmController {

    @Resource
    private QmRulesService qmRulesService;
    @Resource
    private QmTemplateService templateService;
    @Resource
    private JCommonData jCommonData;
    @Resource
    private QmDataService qmDataService;
    @Resource
    private QmGradeService qmGradeService;

    /**
     * 取质检规则LIST
     */
    @GetMapping("/qmRulesList")
    public R<PageData<QmRulesResVo>> qmRulesList(@Validated QmRulesReqVo reqVo){
        return R.SuccessPage(qmRulesService.qmRulesList(reqVo));
    }

    /**
     * 增加质检规则
     */
    @PostMapping("/qmRulesAdd")
    public R<CmdResVo> qmRulesAdd(@RequestBody @Validated QmRulesAddVo addVo){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(qmRulesService.qmRulesAdd(usrid,addVo));
    }

    /**
     * 修改质检规则
     */
    @PostMapping("/qmRulesEdit")
    public R<CmdResVo> qmRulesEdit(@RequestBody @Validated QmRulesAddVo addVo){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(qmRulesService.qmRulesEdit(usrid,addVo));
    }

    /**
     * 删除质检规则
     */
    @PostMapping("/qmRulesDelete")
    public R<CmdResVo> qmRulesDelete(@RequestBody @Validated QmRulesDelVo delVo){
        return R.Success(qmRulesService.qmRulesDelete(delVo.getRulesid()));
    }

    /**
     * 取质检模板LIST
     */
    @GetMapping("/qmTempList")
    public R<PageData<QmTempResVo>> qmTempList(@Validated QmTempReqVo reqVo){
        return R.SuccessPage(templateService.qmTempList(reqVo));
    }

    /**
     * 增加质检模板
     */
    @PostMapping("/qmTempAdd")
    public R<CmdResVo> qmTempAdd(@RequestBody @Validated QmTempAddVo addVo){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(templateService.qmTempAdd(usrid,addVo));
    }

    /**
     * 修改质检模板
     */
    @PostMapping("/qmTempEdit")
    public R<CmdResVo> qmTempEdit(@RequestBody @Validated QmTempAddVo addVo){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(templateService.qmTempEdit(usrid,addVo));
    }

    /**
     * 删除质检模板
     */
    @PostMapping("/qmTempDelete")
    public R<CmdResVo> qmTempDelete(@RequestBody @Validated QmTempDelVo delVo){
        return R.Success(templateService.qmTempDelete(delVo.getTempid()));
    }

    /**
     * 通过模板取规则
     */
    @GetMapping("/getRulesList")
    public R<List<LabelResVo>> getRulesList(@RequestParam @NotNull Integer tempid){
        return R.Success(qmRulesService.getRulesList(tempid));
    }

    /**
     * 通过模板取规则详细
     */
    @GetMapping("/getRulesDetailList")
    public R<List<QmRulesResVo>> getRulesDetailList(@RequestParam @NotNull Integer tempid){
        return R.Success(qmRulesService.getRulesDetailList(tempid));
    }

    /**
     * 取质检数据预览
     */
    @PostMapping("/preViewQmData")
    public R<PageData<QmViewResVo>> preViewQmData(@RequestBody @Validated PreViewTrigReqVo trigReqVo){
        return R.SuccessPage(qmDataService.preViewQmData(trigReqVo));
    }

    /**
     * 生成待质检数据
     */
    @PostMapping("/importQmData")
    public R<ImpResultVo> importQmData(@RequestBody @Validated ImpQmDataReqVo qmDataReqVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(qmDataService.importQmData(usrid,qmDataReqVo));
    }

    /**
     * 取质检数据
     */
    @PostMapping("/qmDataList")
    public R<PageData<QmCaseResVo>> qmDataList(@RequestBody @Validated QmCaseReqVo reqVo){
        JSysUser user = jCommonData.getCurrUsr();
        return R.SuccessPage(qmDataService.qmDataList(user,reqVo,true));
    }

    /**
     * 质检数据导出
     */
    @PostMapping("/expQmData")
    public R<CmdResVo> expQmData(@RequestBody @Validated QmCaseReqVo reqVo, HttpServletResponse response){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(qmDataService.expQmData(user,reqVo,response));
    }

    /**
     * 取质检得分明细项
     */
    @GetMapping("/qmGraceItem")
    public R<List<QmGradeResVo>> qmGraceItem(@RequestParam @NotNull String dataid){
        return R.Success(qmGradeService.getQmGradeItem(dataid));
    }

    /**
     * 复检提交
     */
    @PostMapping("/qmCheckResult")
    public R<CmdResVo> qmCheckResult(@RequestBody @Validated QmCheckVo checkVo){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(qmDataService.checkResult(usrid,checkVo));
    }

    /**
     * 获取复检结果
     */
    @GetMapping("/qmGetCheckResult")
    public R<QmCheckResultResVo> qmGetCheckResult(@RequestParam @NotNull String dataid){
        String usrid=jCommonData.getCurrUsrID();
        return R.Success(qmDataService.getCheckResult(usrid,dataid));
    }

    /**
     * 上载录音质检提交
     */
    @PostMapping("/uploadResult")
    public R<ImpResultVo> uploadResult(@RequestBody @Validated ImpQmDataReqVo resVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(qmDataService.uploadResult(user,resVo));
    }

    /**
     * 取质检方案列表
     */
    @GetMapping("/getTempList")
    public R<List<LabelResVo>> getTempList()
    {
        return R.Success(templateService.getTempList());
    }

    /**
     * 执行AI质检
     */
    @PostMapping("/doAiQm")
    public R<CmdResVo> doAiQm(@RequestBody @Validated QmCaseReqVo resVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(qmDataService.doAiQm(usrid,resVo));
    }

    /**
     * 交换ASR对话角色
     */
    @PostMapping("/swapAsrCallRole")
    public R<CmdResVo> swapAsrCallRole(@RequestBody @Validated QmCaseReqVo resVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(qmDataService.swapAsrCallRole(usrid, resVo.getDataid()));
    }
}
